package com.edu.lxshpc.controller;

import com.aliyuncs.exceptions.ClientException;
import com.edu.lxshpc.service.MusicService;
import com.edu.lxshpc.service.PhraseService;
import com.edu.lxshpc.utils.AliyunOssUtils;
import com.edu.lxshpc.utils.Result;
import com.rabbitmq.client.impl.CredentialsProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * 其他类接口
 * @author zgh
 * @date 2023/11/13
 */
@RestController
@RequestMapping("/other")
@Slf4j
public class OtherController {
    @Autowired
    private MusicService musicService;
    @Autowired
    private PhraseService phraseService;
    /**
     * 获取音乐列表
     * @return Resultmusic集合
     */
    @GetMapping("/musics")
    public Result list(){
        return musicService.getMusicList();
    }

    /**
     * 获取所有短语
     * @return Result对象，包含短语列表
     */
    @GetMapping("/phrases")
    public Result phrases(){
        return phraseService.getPhrase();
    }

    @PreAuthorize("hasAnyRole('USER','ADMIN')")
    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws ClientException {
        String originalFilename = file.getOriginalFilename();
        String suff = originalFilename.split("\\.")[1];
        String name = UUID.randomUUID()+ "." + suff;
        return AliyunOssUtils.upload(name,file);
    }
}
